package com.hwd.srb.common.exception;

import com.hwd.srb.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/11 20:10
 * @Description 自定义断言类
 */
@Slf4j
public abstract class Assert {

    /**
     * 断言对象不为空
     * 如果对象为空，抛出异常
     *
     * @param object       待判对象
     * @param responseEnum 异常状态枚举
     */
    public static void notNull(Object object, ResponseEnum responseEnum) {
        if (object == null) {
            log.info("object is null......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言对象不为空
     * 如果对象为空，抛出异常
     *
     * @param object  待判对象
     * @param message 异常信息
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            log.info("object is null......");
            throw new BusinessException(message);
        }
    }

    /**
     * 断言对象为空
     * 如果对象object不为空，则抛出异常
     *
     * @param object       待判对象
     * @param responseEnum 异常状态枚举
     */
    public static void isNull(Object object, ResponseEnum responseEnum) {
        if (object != null) {
            log.info("object is not null......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression   是否成功
     * @param responseEnum 异常状态枚举
     */
    public static void isTrue(boolean expression, ResponseEnum responseEnum) {
        if (!expression) {
            log.info("fail...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression 是否成功
     * @param message    异常提示信息
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            log.info("fail...............");
            throw new BusinessException(message);
        }
    }

    /**
     * 断言两个对象不相等
     * 如果相等，则抛出异常
     *
     * @param obj1         待判对象1
     * @param obj2         待判对象2
     * @param responseEnum 异常状态枚举
     */
    public static void notEquals(Object obj1, Object obj2, ResponseEnum responseEnum) {
        if (obj1.equals(obj2)) {
            log.info("equals...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象相等
     * 如果不相等，则抛出异常
     *
     * @param obj1         待判对象1
     * @param obj2         待判对象2
     * @param responseEnum 异常状态枚举
     */
    public static void equals(Object obj1, Object obj2, ResponseEnum responseEnum) {
        if (!obj1.equals(obj2)) {
            log.info("not equals...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     *
     * @param param        待判参数
     * @param responseEnum 异常状态枚举
     */
    public static void notEmpty(String param, ResponseEnum responseEnum) {
        if (StringUtils.isEmpty(param)) {
            log.info("is empty...............");
            throw new BusinessException(responseEnum);
        }
    }
}
